%%% Code for assigning estimated structural parameters to interpretable
%%% categories 
%%% Works both with only factors, adding risk parameters, and adding time


% factor loadings
est_load=zeros(max(c_char_n),factor_n);
% intrecepts
est_alpha=zeros(X_n, factor_n);
% standard deviations
est_sd=zeros(factor_n,1);
% mean
est_f_mean=zeros(factor_n,1);
% standard deviation of continuous measure
est_sd_cont=nan(max(cont_char_n), factor_n);


if size(theta,1)>=size_factors
    %%% coeffs associated with the factor part of the estimation 
    for i=1:factor_n
        est_alpha(:,i)=theta(1:X_n,1);
        theta(1:X_n)=[];
        size_const1=b_char_n(i)+thresh(i)*m_char_n(i)+cont_char_n(i);
        % in addition the standard deviation of the factor distribution is to
        % be estimated (the factor is common to all measures)
        % take the exponent to ensure positive values. Can keep the same
        % threshold value for assignment so long as the constant
        % (threshold-mean for a given measure) are estimated and allowed to
        % vary BY measure. Same as for a binomial case, the choice of the
        % threshold at 0 is arbitrary and thus the coefficient on the "mean"
        % includes the measure mean minus the threshold in reality (check in
        % binary simulation by allowing thresholds to vary).
        est_f_mean(i,1)=theta(size_const1);
        est_load(:,i)=[ [1 theta(size_const1+1:size_const1+c_char_n(i)-1)']' ; NaN(load_size-c_char_n(i),1)];
        est_sd(i,1)=exp(theta(size_const1+c_char_n(i)));
        theta(1:size_const1+c_char_n(i))=[];  
        if cont_char_n(i)>0
            est_sd_cont(:,i)=exp(theta(1:cont_char_n(i)));
            theta(1:cont_char_n(i))=[];    
        end
    end
end

%%% exceutes only if risk paras are also included
if size(theta,1)==size_risk

    % estimated parameters for coefficient of risk aversion
    est_thetas=theta(1:factor_n+1);
    theta(1:factor_n+1)=[];

    % estimated parameters for mistakes
    est_kappas=theta(1:factor_n+1);
    theta(1:factor_n+1)=[];

    % estimated parameters for standard deviation on coefficient of risk
    % aversion (apparent inconsistency of risk preferences i.e., imperfect
    % self-knowledge on risk domain)
    est_sig_ths=theta(1:factor_n+1);
    theta(1:factor_n+1)=[];
    

    
    %%% unobserved type coefficients
    est_types=NaN(3,n_type);
    %%% estimated type prevalences
    est_p_types=zeros(n_type,1);
    for tt=1:(n_type)
        if tt<n_type
            est_types(:,tt)=theta(1:3);
            est_p_types(tt)=normcdf(theta(4));
            theta(1:4)=[];  
        else
            est_types(:,tt)=theta(1:3);
            est_p_types(tt)=1-sum(est_p_types);
            theta(1:3)=[];  
        end
    end
    

    
    %%% executes only if time paras are also included
elseif size(theta,1)==(size_risk+size_time)
    
    % estimated parameters for coefficient of risk aversion
    est_thetas=theta(1:factor_n+1);
    theta(1:factor_n+1)=[];

    % estimated parameters for mistakes
    est_kappas=theta(1:factor_n+1);
    theta(1:factor_n+1)=[];

    % estimated parameters for standard deviation on coefficient of risk
    % aversion (apparent inconsistency of risk preferences i.e., imperfect
    % self-knowledge on risk domain)
    est_sig_ths=theta(1:factor_n+1);
    theta(1:factor_n+1)=[];
    
    % estimated parameters for discount rate
    est_rs=theta(1:factor_n+1);
    theta(1:factor_n+1)=[];

    % estimated parameters for standard deviation on discount rate
    % (apparent inconsistency of risk preferences i.e., imperfect
    % self-knowledge on time domain)
    est_sig_rs=theta(1:factor_n+1);
    theta(1:factor_n+1)=[];
    
    % if mistake parameter has separate intercept on time domain
    if K_time==1
        est_K_time=theta(end);
        theta(end)=[];
    end




    %%% unobserved type coefficients
    est_types=NaN(5,n_type);
    %%% estimated type prevalences
    est_p_types=zeros(n_type,1);
    for tt=1:(n_type)
        if tt<n_type
            est_types(:,tt)=theta(1:5);
            est_p_types(tt)=normcdf(theta(6));
            theta(1:6)=[];  
        else
            est_types(:,tt)=theta(1:5);
            est_p_types(tt)=1-sum(est_p_types);
            theta(1:5)=[]; 
        end
    end
    

    

end